package com.reporter.object;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import jsystem.framework.FrameworkOptions;
import jsystem.framework.JSystemProperties;
import jsystem.framework.report.ExtendTestListener;
import jsystem.framework.report.ExtendTestReporter;
import jsystem.framework.report.TestInfo;
import jsystem.framework.scenario.JTestContainer;
import jsystem.framework.scenario.flow_control.AntForLoop;
import junit.framework.AssertionFailedError;
import junit.framework.Test;

public class PerfPublisher implements ExtendTestReporter, ExtendTestListener {

	JAXBContext context;
	Marshaller m;
	Report report;
	ArrayList<com.reporter.object.Test> tests;
	com.reporter.object.Test newTest;
	private String passed = "yes";
	private int index = 0;
	private long testStart;

	public PerfPublisher() {
		init();
	}

	@Override
	public boolean asUI() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init() {
		tests = new ArrayList<com.reporter.object.Test>();
		tests.clear();
		report = new Report();
		report.setName("THIS IS A TEST");
		report.setStart(report.getStartTime());
	}

	@Override
	public void initReporterManager() throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void report(String arg0, String arg1, boolean arg2, boolean arg3) {
		newTest.setDescription("report 1");

	}

	@Override
	public void report(String arg0, String arg1, int arg2, boolean arg3) {
		newTest.setDescription("report 2");

	}

	@Override
	public void addError(Test test, Throwable t) {
		passed = "no";
	}

	@Override
	public void addFailure(Test test, AssertionFailedError t) {
		passed = "no";

	}

	@Override
	public void endTest(Test test) {
		Success success = new Success();
		success.setPassed(passed);
		Result r = new Result();
		r.setSuccess(success);
		newTest.setResult(r);
		tests.add(newTest);

	}

	@Override
	public void startTest(Test test) {
		passed = "yes";
	}

	@Override
	public void addWarning(Test arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endContainer(JTestContainer arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endLoop(AntForLoop arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endRun() {
		report.setTest(tests);
		report.setEnd(report.getEndTime());
		try {
			context = JAXBContext.newInstance(Report.class);
			m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
			final String currentLogFolder = JSystemProperties.getInstance()
					.getPreference(FrameworkOptions.LOG_FOLDER)
					+ File.separator + "current";
			File xmlFile = new File(currentLogFolder + File.separator
					+ "xml_report" + (++index) + ".xml");
			if (xmlFile.exists()) {
				xmlFile.delete();
			}
			m.marshal(report, xmlFile);
			tests.clear();
		} catch (JAXBException e) {
			System.out.println("Failed closing CSV report");
		}
	}

	@Override
	public void startContainer(JTestContainer arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startLoop(AntForLoop arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startTest(TestInfo arg0) {
		newTest = new com.reporter.object.Test();
		newTest.setName(arg0.methodName);
		testStart = System.currentTimeMillis();
		passed = "yes";

	}

	@Override
	public void addProperty(String arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void endSection() {
		// TODO Auto-generated method stub

	}

	@Override
	public void flush() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void report(String arg0, String arg1, int arg2, boolean arg3,
			boolean arg4, boolean arg5) {
		newTest.setDescription("report 3");
	}

	@Override
	public void saveFile(String arg0, byte[] arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setContainerProperties(int arg0, String arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setData(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void startSection() {
		// TODO Auto-generated method stub

	}

}
